#include <QtGui>
#include "docpage.h"
#include "video.h"
#include "doccontrol.h"
#include "infoview.h"
#include "qextserialport.h"

DocPage::DocPage(QextSerialPort* port, QWidget* parent):
  QWidget(parent)
{
  QFrame *frame = new QFrame;
  frame->setFrameStyle(QFrame::Panel);
  frame->setFrameShadow(QFrame::Sunken);
  QVBoxLayout* vbox1 = new QVBoxLayout;
  m_port = port;
  m_inMode = 0;

  doc = new DocWidget();
  vbox1->addWidget(doc);
  frame->setLayout(vbox1);

  QHBoxLayout* box = new QHBoxLayout;
  box->addWidget(frame,7);

  QVBoxLayout* vbox = new QVBoxLayout;
  DocControl* docView = new DocControl;
  vbox->addWidget(docView,7);
  InfoView* infoView = new InfoView;
  vbox->addWidget(infoView,3);

  box->addLayout(vbox,2);
  setLayout(box);
  connect(docView, SIGNAL(setEmergent(int)), this, SLOT(setEmergent(int)));
  connect(docView, SIGNAL(setIncident(int)), this, SLOT(setIncident(int)));
  connect(docView, SIGNAL(setFilter(int)), doc, SLOT(setFilter(int)));
  connect(docView, SIGNAL(setFactor(double)), doc, SLOT(setFactor(double)));
  connect(docView, SIGNAL(snapshot()), doc, SLOT(snap()));
  connect(doc, SIGNAL(drawHist(int*,int*,int*)), 
	  infoView, SLOT(drawHist(int*, int*, int*)));
}

  void DocPage::setEmergent(int mode)
  {
	  char c[2];
	  switch(mode){
		  case 0: c[0]=0x96;
			  break;
		  case 1: c[0]=0x93;
			  break;
		  case 2: c[0]=0x99;
			  break;
	  }
	  m_port->write(c,1);
  }

  void DocPage::setIncident(int mode)
  {
	  char c[2];
	  switch(m_inMode){
		  case 1: c[0]=0x45;
			  break;
		  case 2: c[0]=0x4a;
			  break;
		  case 3: c[0]=0x55;
			  break;
		  case 4: c[0]=0x5a;
			  break;
		  case 5: c[0]=0x65;
			  break;
		  case 6: c[0]=0x6a;
			  break;
		  case 7: c[0]=0x75;
			  break;
		  case 8: c[0]=0x7a;
			  break;
	  }
	  if(m_inMode!=0)m_port->write(c,1);

	  switch(mode){
		  case 0: c[0]=0;
			  break;
		  case 1: c[0]=0xb5;
			  break;
		  case 2: c[0]=0xba;
			  break;
		  case 3: c[0]=0xa5;
			  break;
		  case 4: c[0]=0xaa;
			  break;
		  case 5: c[0]=0x95;
			  break;
		  case 6: c[0]=0x9a;
			  break;
		  case 7: c[0]=0x85;
			  break;
		  case 8: c[0]=0x8a;
			  break;
	  }
	  m_port->write(c,1);
	  m_inMode = mode;
  }

